package b1.mobile.blackberry.screen;

import java.util.Vector;

import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.decor.BackgroundFactory;
import net.rim.device.api.util.StringProvider;
import b1.mobile.blackberry.dao.BusinessPartnerDAO;
import b1.mobile.blackberry.dao.DataAccessDelegate;
import b1.mobile.blackberry.entity.BusinessPartnerEntity;
import b1.mobile.blackberry.io.http.Serviceworker;
import b1.mobile.blackberry.lang.CommonConstant;
import b1.mobile.blackberry.ui.component.IconListField;
import b1.mobile.blackberry.ui.component.SearchBarField;
import b1.mobile.blackberry.ui.component.util.FieldFactory;
import b1.mobile.blackberry.ui.component.util.ThemeUtil;

public class BPListScreen extends BaseFunctionScreen implements DataAccessDelegate{

	private static String _title = "Business Partner";
	
	private IconListField _listField;
	private SearchBarField searchField;
	
	String keyword = "";
	int pageIndex = 0;
	int pageSize = 25;
	int total = 0;
	
	MenuItem _loadMore = new MenuItem(new StringProvider("Load More"), 0, 0){
		public void run() {
			loadMore();
		}
	};
	
	Manager backManager = new VerticalFieldManager();
	/**
	 * 
	 */
	public BPListScreen() {
		setTitle(FieldFactory.makeScreenTitle(_title));
		ThemeUtil.decorateScreen(this);
		
		backManager.setMargin(10, 0, 10, 0);
		
		backManager.setBackground(BackgroundFactory.createSolidBackground(0xf3f3f3));
		
		searchField = new SearchBarField();
		searchField.getSearchButton().setChangeListener(new FieldChangeListener() {
			public void fieldChanged(Field field, int context) {
				onSearch();
			}
		});
		
		backManager.add(searchField);		
		
		_listField = new IconListField();
		_listField.setListType(CommonConstant.ICON_LIST_TYPE_BS_LIST);
		_listField.setRowHeight(_listField.getRowHeight()*25/10);
		backManager.add(_listField);
		
		this.add(backManager);
	}

	protected void onUiEngineAttached(boolean attached) {
		if (attached){						
			BusinessPartnerDAO dao = new BusinessPartnerDAO(this);
			dao.getTotal(keyword);
			dao.list(keyword, pageIndex, pageSize);
		}
	}
	
	protected void makeMenu(Menu menu, int instance) {
		menu.addSeparator();
		menu.add(_loadMore);
	}
	
	protected void onSearch() {
		backManager.delete(_listField);
		
		_listField = new IconListField();
		_listField.setListType(CommonConstant.ICON_LIST_TYPE_BS_LIST);
		_listField.setRowHeight(_listField.getRowHeight()*25/10);
		backManager.add(_listField);
		
		keyword = searchField.getEditField().getText();
		pageIndex = 0;
		
		BusinessPartnerDAO dao = new BusinessPartnerDAO(this);
		dao.list(keyword, pageIndex, pageSize);
		dao.getTotal(keyword);
	}
	
	protected boolean navigationClick(int status, int time) {
		if (getLeafFieldWithFocus() instanceof IconListField){
			Object item = _listField.get(_listField, _listField.getSelectedIndex());
			if (item instanceof BusinessPartnerEntity){
				BusinessPartnerEntity ety = (BusinessPartnerEntity)item;
				getUiEngine().pushScreen(new BPInfoScreen(ety.getCode()));
			}
		} else if(getLeafFieldWithFocus() == searchField.getSearchButton()){
			onSearch();
		} else {
			super.navigationClick(status, time);
		}
		return true;
	}
	
	public void onDataReady(Serviceworker worker, Object data){
		super.onDataReady(worker, data);
		if(worker.responseCode == 200){
			if(worker.getOperator().equals(BusinessPartnerDAO.OP_LIST)){
				final Vector list = (Vector)data;
				UiApplication.getUiApplication().invokeLater(new Runnable(){
					public void run(){
						bindList(list);
					}
				});
			} else if(worker.getOperator().equals(BusinessPartnerDAO.OP_TOTAL)){
				total = ((Integer)data).intValue();
				UiApplication.getUiApplication().invokeLater(new Runnable(){
					public void run(){
						updateTitle();
					}
				});
			}
		}
	}
	
	private void updateTitle() {
		if(total != 0 && _listField.getSize() > 0){
			StringBuffer title = new StringBuffer();
			title.append(_title);
			title.append(" (");
			title.append(_listField.getSize());
			title.append("/");
			title.append(total);
			title.append(")");
			
			setTitle(FieldFactory.makeScreenTitle(title.toString()));
		} else {
			setTitle(FieldFactory.makeScreenTitle(_title));
		}
	}

	public void bindList(Vector list){
		int baseRow = _listField.getSize();
		for(int i = 0; i < list.size(); i++){
			_listField.insert(baseRow + i, (BusinessPartnerEntity)list.elementAt(i));
		}
		updateTitle();
	}
	
	protected void loadMore() {
		if(total != 0 && (pageIndex * pageSize) < total && _listField.getSize() < total){
			pageIndex ++;
			
			BusinessPartnerDAO dao = new BusinessPartnerDAO(this);
			dao.list(keyword, pageIndex, pageSize);
		}
	}
}
